$(function () {

    // 1.获取当前登录的用户
    get("auth/getUser", function (resp) {

        // 获取购物车数量
        var count = resp.userCarCount;

        var html = "";
        if (resp.data) {
            console.log(resp.data);
            var username = resp.data.username;
            html += "<p class=\"t-con-l f-l\">" +
                "            <span> 欢迎:【" + username + "】登录</span>\n" +
                "            <a href=\"javascript:void(0)\" onclick=\"logout()\">注销</a>\n" +
                "        </p>\n";
        } else {
            html += "        <p class=\"t-con-l f-l\" th:if=\"${session.loginUser == null}\">\n" +
                "            您还没有登录，请<a onclick='toLogin()' href=\"javascript:void(0)\">登录</a>\n" +
                "        </p>";
            ;
        }
        html += "        <ul class=\"t-con-r f-r\">\n" +
            "            <li><a href=\"#\">我 (个人中心)</a></li>\n" +
            "            <li><a href=\"http://localhost:81/shopcar/getUserCarList\">我的购物车<span style='color: red'>("+count+")</span></a></li>\n" +
            "            <li><a href=\"#\">我的订单</a></li>\n" +
            "            <li class=\"erweima\">\n" +
            "                <a href=\"#\">扫描二维码</a>\n" +
            "                <div class=\"ewm-tu\">\n" +
            "                    <a href=\"#\"><img src=\"images/erweima-tu.jpg\" /></a>\n" +
            "                </div>\n" +
            "            </li>\n" +
            "            <div style=\"clear:both;\"></div>\n" +
            "        </ul>\n" +
            "        <div style=\"clear:both;\"></div>";
        $("#shopTitle").html(html);
    });

});
// 注销用户登录
function logout(){
    get("auth/logout",function(resp){
        if(resp.code == 0){
            location.reload();
        }
    });
}
// 用户登录后跳转到用户浏览的页面
function toLogin() {
    // 获取当前页面地址
    var returnUrl = location.href;
    // 2.跳转到登录页面
    location.href = "http://localhost:81/auth/loginUser.html?returnUrl=" + returnUrl;
}
